$("#password").keyup(function (event) {
    if (event.keyCode === 13) {
        login();
    }
});
$("#uword").keyup(function (event) {
    if (event.keyCode === 13) {
        login();
    }
});
/*登录js*/
function login() {
    var name = $("#username").val();
    var password = $("#password").val();
    if (name.length === 0) {
        return;
    }
    if (password.length === 0) {
        return;
    }
    $.ajax({
        url: "/admin/login",
        type: "post",
        data: {"username": name, "password": password},
        dataType: "json",
        async: false,
        success: function (data) {
            if (data.success == true) {
                $.toast({
                    text: data.message,
                    heading: '提示',
                    icon: 'success',
                    showHideTransition: 'plain',
                    allowToastClose: true,
                    hideAfter: 1000,
                    stack: 1,
                    position: 'bottom-center',
                    textAlign: 'center',
                    loader: true,
                    loaderBg: '#ffffff',
                    bgColor: '#0e0411',
                    xOffset: {top: 100, left: -20},

                    afterHidden: function () {
                        window.location.href = "/admin/index";
                    }
                });
            } else {
                showMsg(data.message, "error", 1000)
            }

        }
    })
}
/*注册页面跳转*/
function registerpage() {
    window.location.href = "/admin/registerpage";
}
/*注册*/
function register(){
    var name = $("#uname").val();
    var password = $("#uword").val();
    if (name.length === 0) {
        return;
    }
    if (password.length === 0) {
        return;
    }
    $.ajax({
        url: "/admin/register",
        type: "post",
        data: {"username": name, "password": password},
        dataType: "json",
        async: false,
        success: function (data) {
            if (data.success == true) {
                $.toast({
                    text: data.message,
                    heading: '提示',
                    icon: 'success',
                    showHideTransition: 'plain',
                    allowToastClose: true,
                    hideAfter: 3000,
                    stack: 3,
                    position: 'bottom-center',
                    textAlign: 'center',
                    loader: true,
                    loaderBg: '#ffffff',
                    bgColor: '#0e0411',
                    xOffset: {top: 100, left: -20},
                    afterHidden: function () {
                        window.location.href = "/admin/index";
                    }
                });
            } else {
                showMsg(data.message, "error", 3000)
            }

        }
    })
}
/*登出*/
function loginOut() {
    $.get('/admin/exit');
    window.location.href = "/admin/index";
    if (name.length === 0) {
        return;
    }
    if (password.length === 0) {
        return;
    }
    $.ajax({
        url: "/admin/login",
        type: "post",
        data: {"username": name, "password": password},
        dataType: "json",
        async: false,
        success: function (data) {
            if (data.success == true) {
                $.toast({
                    text: data.message,
                    heading: '提示',
                    icon: 'success',
                    showHideTransition: 'plain',
                    allowToastClose: true,
                    hideAfter: 1000,
                    stack: 1,
                    position: 'bottom-center',
                    textAlign: 'center',
                    loader: true,
                    loaderBg: '#ffffff',
                    bgColor: '#0e0411',
                    xOffset: {top: 100, left: -20},

                    afterHidden: function () {
                        window.location.href = "/admin/index";
                    }
                });
            } else {
                showMsg(data.message, "error", 1000)
            }

        }
    })
}
/*控制中心跳转*/
function locationControl() {


}

/*等待框*/
function loadingText(btnId, text) {
    $("#" + btnId + "").attr("disabled", "");
    $("#" + btnId + "").text(text);
}

/*错误信息打印*/
function showMsg(text, icon, hideAfter) {
    $.toast({
        text: text,
        heading: 'Tips',
        icon: icon,
        showHideTransition: 'slide',
        allowToastClose: true,
        hideAfter: hideAfter,
        stack: 3,
        position: 'bottom-center',
        textAlign: 'center',
        loader: true,
        loaderBg: '#2dadd2',
    });
}
